﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using CDA = System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using System.Web;
using System.Web.DynamicData;
using SWD = System.Web.DynamicData;
using System.Web.UI.WebControls;
using Dimok;
using TelerikExtensions;

namespace Data.LINQ {

	#region Attributes

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Values that represent JQueryEditTemplates. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public enum JQueryEditTemplates { text = 0, dropdown = 1, textline = 2 }

	#region Table Level

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for action table. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class ActionTableAttribute : Attribute {
		public Type TableType { get; set; }
		public ActionTableAttribute() { }
		public ActionTableAttribute(Type TableType) {
			this.TableType = TableType;
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for update table. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class UpdateTableAttribute : ActionTableAttribute {
		public UpdateTableAttribute() { }
		public UpdateTableAttribute(Type TableType) : base(TableType) { }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for delete table. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class DeleteTableAttribute : ActionTableAttribute {
		public DeleteTableAttribute() { }
		public DeleteTableAttribute(Type TableType) : base(TableType) { }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for insert table. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class InsertTableAttribute : ActionTableAttribute {
		public InsertTableAttribute() { }
		public InsertTableAttribute(Type TableType) : base(TableType) { }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for select table. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class SelectTableAttribute : Attribute {
		public string DefaultFilter { get; private set; }
		/// <summary>
		/// Table can be used in Table Control
		/// </summary>
		public SelectTableAttribute() : this(false) { }
		/// <param name="ShowAll">Default: false</param>
		public SelectTableAttribute(bool ShowAll) : this(ShowAll ? "1>0" : "1=0") { }
		public SelectTableAttribute(string DefaultFilter) { this.DefaultFilter = DefaultFilter; }
	}
	#endregion

	#region Column Level

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for edit column template. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class EditColumnTemplateAttribute : Attribute {
		private Type LookUpEntityType = null;
		private Type EntityType = null;
		public EditColumnTemplateAttribute() : this(null, null) { }
		public EditColumnTemplateAttribute(Type LookUpEntityType, Type EntityType) {
			this.EntityType = EntityType;
			this.LookUpEntityType = LookUpEntityType;
			this.LookUpEntity = LookUpEntityType == null ? "" : LookUpEntityType.Name;
			Template = LookUpEntityType == null ? JQueryEditTemplates.textline : JQueryEditTemplates.dropdown;
			EditEvent = "dblclick";
			ElementCss = "";
			LookUpTextField = "Name";
			LookUpValueField = "ID";
			IsEdit = true;
			IsEditAll = false;
			IsAdd = true;
		}
		public string ElementCss { get; set; }
		public string EditEvent { get; set; }
		public JQueryEditTemplates Template { get; set; }
		private string _lookUpFieldName;
		public string LookUpFieldName {
			get { return CheckLookup(EntityType, _lookUpFieldName); }
			set { _lookUpFieldName = value; }
		}
		private string _lookUpTextField = "Name";
		public string LookUpTextField {
			get { return CheckLookup(LookUpEntityType, _lookUpTextField); }
			set { _lookUpTextField = value; }
		}
		private string _lookUpValueField = "ID";
		public string LookUpValueField {
			get { return CheckLookup(LookUpEntityType, _lookUpValueField); }
			set { _lookUpValueField = value; }
		}
		public string LookUpEntity { get; set; }
		public bool IsEdit { get; set; }
		public bool IsEditAll { get; set; }
		public bool IsAdd { get; set; }
		private string CheckLookup(Type EntityType, string LookUpField) {
			if (EntityType != null &&
					LookUpField + "" != "" && EntityType.GetProperty(LookUpField) == null
			) throw new NotImplementedException("Entity " + EntityType.Name + " doesn't have property " + LookUpField);
			return LookUpField;
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for look up field. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class LookUpFieldAttribute : Attribute {
		public Type LookUpEntityType { get; set; }
		public LookUpFieldAttribute(Type LookUpEntityType) { this.LookUpEntityType = LookUpEntityType; }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for html title. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class HtmlTitleAttribute : Attribute {
		public string Title { get; private set; }
		public HtmlTitleAttribute(string htmlTitle) { Title = htmlTitle; }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for ordered field. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class OrderedFieldAttribute : Attribute {
		public byte Index { get; private set; }
		public OrderedFieldAttribute(byte Index) {
			this.Index = Index;
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for default field. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class DefaultFieldAttribute : OrderedFieldAttribute {
		public DefaultFieldAttribute(byte Index) : base(Index) { }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for detail field. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class DetailFieldAttribute : OrderedFieldAttribute {
		public DetailFieldAttribute(byte Index) : base(Index) { }
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for sort field. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class SortFieldAttribute : OrderedFieldAttribute {
		public SortDirection Direction { get; private set; }
		public SortFieldAttribute(byte Index) : this(Index, SortDirection.Ascending) { }
		public SortFieldAttribute(byte Index, string Direction) : this(Index, Direction.ConvertEnum<SortDirection>()) { }
		public SortFieldAttribute(byte Index, SortDirection Direction): base(Index) {
			this.Direction = Direction;
		}
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for client key. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class ClientKeyAttribute : Attribute { }

	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Attribute for detail key. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class DetailKeyAttribute : Attribute { }
	#endregion
	#endregion
}